package algo.couterbased;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import algorithm.AbstractCounterBased;

public class LossyCounting extends AbstractCounterBased {
	
	public LossyCounting() {
		super("LossyCounting");
	}

	@Override
	public Map<Object, Double> compute(int[] stream){
		
		size = 0;
		double delta = 0;
		List<Object> t = new ArrayList<Object>();
		result = new HashMap<Object, Double>();
		
		for (Object item : stream){
			size++;
			
			if (t.contains(item)){
				result.put(item, result.get(item) + 1);
			} else {
				t.add(item);
				result.put(item, 1 + delta);
			}
			
			if (size / stream.length != delta){
				delta = size / stream.length;
				for (Object j : t){
					if (result.get(j) < delta){
						t.remove(j);
					}
				}
			}
		}
		
		return result;
	}
}
